package com.woodws.fresh.square.controller;

import com.woodws.fresh.square.api.dto.DynamicDto;
import com.woodws.fresh.square.api.dto.GetDynamicListDto;
import com.woodws.fresh.square.api.service.InvitationService;
import com.woodws.fresh.square.entity.common.Result;
import com.woodws.fresh.square.entity.inputvo.*;
import com.woodws.fresh.square.entity.outputvo.GetAllDynamicListOutputVo;
import com.woodws.fresh.square.entity.outputvo.GetTopDynamicListForUserIdOutputVo;
import com.woodws.fresh.square.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/hf/square")
@Api("InvitationController:动态广场controller")
public class InvitationController {

    @Autowired
    private InvitationService invitationService;

    @PostMapping("/getAllDynamicList")
    @ApiOperation(value = "分页查询所有动态列表",notes = "currentPage为当前页码，pageSize为每页显示数量，都是必输！")
    public Result<GetAllDynamicListOutputVo> getAllDynamicList(@RequestBody @Valid GetAllDynamicListInputVo getAllDynamicListInputVo) {
        GetDynamicListDto getDynamicListDto = invitationService.getAllDynamicList(getAllDynamicListInputVo.getCurrentPage(), getAllDynamicListInputVo.getPageSize());
        GetAllDynamicListOutputVo getAllDynamicListOutputVo = new GetAllDynamicListOutputVo();
        BeanUtils.copyProperties(getDynamicListDto,getAllDynamicListOutputVo);
        return ResultUtil.success(getAllDynamicListInputVo);
    }

    @PostMapping("/getDynamicListForUserId")
    @ApiOperation(value = "根据用户ID分页查询所有动态列表",notes = "currentPage为当前页码，pageSize为每页显示数量，userId为用户编号，都是必输！")
    public Result<GetAllDynamicListOutputVo> getDynamicListForUserId(@RequestBody @Valid GetDynamicListForUserIdInputVo getDynamicListForUserIdInputVo){
        GetDynamicListDto dynamicListForUserId = invitationService.getDynamicListForUserId(getDynamicListForUserIdInputVo.getCurrentPage(), getDynamicListForUserIdInputVo.getPageSize(), getDynamicListForUserIdInputVo.getUserId());
        GetAllDynamicListOutputVo getAllDynamicListOutputVo = new GetAllDynamicListOutputVo();
        BeanUtils.copyProperties(dynamicListForUserId, getAllDynamicListOutputVo);
        return ResultUtil.success(getAllDynamicListOutputVo);
    }

    @PostMapping("/getTopDynamicListForUserId")
    @ApiOperation(value = "根据用户ID查询置顶列表",notes = "userId为用户编号，必输项")
    public Result<GetTopDynamicListForUserIdOutputVo> getTopDynamicListForUserId(@RequestBody @Valid GetTopDynamicListForUserIdInputVo getTopDynamicListForUserIdInputVo){
        GetDynamicListDto topDynamicListForUserId = invitationService.getTopDynamicListForUserId(getTopDynamicListForUserIdInputVo.getUserId());
        GetTopDynamicListForUserIdOutputVo getTopDynamicListForUserIdOutputVo = new GetTopDynamicListForUserIdOutputVo();
        getTopDynamicListForUserIdOutputVo.setDynamicList(topDynamicListForUserId.getDynamicList());
        return ResultUtil.success(getTopDynamicListForUserIdOutputVo);
    }

    @PostMapping("/deleteDynamic")
    @ApiOperation(value = "根据动态ID删除对应动态",notes = "dynamicId为当前动态ID，必输项")
    public Result deleteDynamic(@RequestBody @Valid DeleteDynamicInputVo deleteDynamicInputVo){
        Integer deleteCount = invitationService.deleteDynamic(deleteDynamicInputVo.getDynamicId());
        if (deleteCount < 1){
            return ResultUtil.failure();
        }
        return ResultUtil.success();
    }

    @PostMapping("/publishDynamic")
    @ApiOperation(value = "新增或转发动态")
    public Result publishDynamic(@RequestBody @Valid PublishDynamicInputVo publishDynamicInputVo){
        DynamicDto dynamicDto = new DynamicDto();
        BeanUtils.copyProperties(publishDynamicInputVo, dynamicDto);
        Integer count = invitationService.publishDynamic(dynamicDto);
        if (count <= 1){
            return ResultUtil.failure();
        }
        return ResultUtil.success();
    }

    @PostMapping("/giveALikeDynamic")
    @ApiOperation(value = "点赞")
    public Result giveALikeDynamic(@RequestBody @Valid DeleteDynamicInputVo deleteDynamicInputVo){
        Integer count = invitationService.giveALikeDynamic(deleteDynamicInputVo.getDynamicId());
        if (count < 1){
            return ResultUtil.failure();
        }
        return ResultUtil.success();
    }

    @PostMapping("/ungiveALikeDynamic")
    @ApiOperation(value = "取消点赞")
    public Result ungiveALikeDynamic(@RequestBody @Valid DeleteDynamicInputVo deleteDynamicInputVo){
        Integer count = invitationService.ungiveALikeDynamic(deleteDynamicInputVo.getDynamicId());
        if (count < 1){
            return ResultUtil.failure();
        }
        return ResultUtil.success();
    }

    @PostMapping("/giveADislikeDynamic")
    @ApiOperation(value = "踩")
    public Result giveADislikeDynamic(@RequestBody @Valid DeleteDynamicInputVo deleteDynamicInputVo){
        Integer count = invitationService.giveADislikeDynamic(deleteDynamicInputVo.getDynamicId());
        if (count < 1){
            return ResultUtil.failure();
        }
        return ResultUtil.success();
    }

    @PostMapping("/ungiveADislikeDynamic")
    @ApiOperation(value = "取消踩")
    public Result ungiveADislikeDynamic(@RequestBody @Valid DeleteDynamicInputVo deleteDynamicInputVo){
        Integer count = invitationService.ungiveADislikeDynamic(deleteDynamicInputVo.getDynamicId());
        if (count < 1){
            return ResultUtil.failure();
        }
        return ResultUtil.success();
    }

    
}
